//
//  MKLineComicDetailViewController.m
//  ManKeCartoon
//
//  Created by 邹功梁 on 15/10/22.
//  Copyright © 2015年 ManKe. All rights reserved.
//

#import "MKLineComicDetailViewController.h"
#import "DataUtils.h"
#import "StringWithHeight.h"
@interface MKLineComicDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    UIView *_orangeView;
}
@end

@implementation MKLineComicDetailViewController
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewWillAppear:(BOOL)animated
{
    _statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    [_statusBarView setBackgroundColor:[UIColor whiteColor]];
    [self.navBarView addSubview:_statusBarView];
    self.tabBarController.tabBar.hidden = YES;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        _comicListArray = [[NSMutableArray alloc] init];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self getData];
    
    [self customNavBar];
}
#pragma mark - GetData
- (void)getData
{
//    http://m.ac.qq.com/LightComic/getComicInfo?t=1445428037560&id=534882
    [AFNETWORK getDataWithNoCacheString:[NSString stringWithFormat:@"http://m.ac.qq.com/LightComic/getComicInfo?t=1445428037560&id=%@", _lightComicModel.comicID] block:^(id block) {
       
        _detailModel = [[MKLightComicDetailModel alloc] initWithDictionary:[block objectForKey:@"data"]];
        _detailModel.detailID = [[block objectForKey:@"data"] objectForKey:@"id"];
        
        for (NSDictionary *dic in [[block objectForKey:@"data"] objectForKey:@"list"])
        {
            MKLineComicListModel *model = [[MKLineComicListModel alloc] initWithDictionary:dic];
            [_comicListArray addObject:model];
        }
        
        [self setTableView];
        [self setUpDetailView];
        
    }];
}
#pragma mark - CustomNavBar
- (void)customNavBar
{
    self.navBarView.backgroundColor = [UIColor clearColor];
    
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setFrame:CGRectMake(10, 23, 30, 30)];
    [_backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    _backButton.layer.masksToBounds = YES;
    _backButton.layer.cornerRadius = 15.;
    _backButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.3];
    [_backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.navBarView addSubview:_backButton];
    
}
#pragma mark - 返回按钮点击方法
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setTableView
{
    _detailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT - 20) style:UITableViewStylePlain];
    _detailTableView.dataSource = self;
    _detailTableView.delegate = self;
    
    _detailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view insertSubview:_detailTableView atIndex:0];
}
- (void)setUpDetailView
{
    NSString *brief_intrdStr = [NSString stringWithFormat:@"作品介绍: %@", _detailModel.brief_intrd];
    
    CGFloat height = [StringWithHeight HeightWithText:brief_intrdStr width:SCREEN_WIDTH - 20 font:[UIFont systemFontOfSize:15.]];
    
    _detailView = [[MKLineComicDetailView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180 * HEIGHT + 80 * HEIGHT + height + 10 * HEIGHT) height:height];
    [_detailView setDetailModle:_detailModel];
    _detailTableView.tableHeaderView = _detailView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _comicListArray.count + 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60 * HEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"reuseIdentifier";
    MKLineComicListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[MKLineComicListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.orangeView.hidden = YES;
    if (indexPath.row == 0) {
        cell.listLabel.text = @" 目录";
        cell.orangeView.hidden = NO;
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    }
    else
    {
        MKLineComicListModel *model = [_comicListArray objectAtIndex:indexPath.row - 1];
        [cell setListModel:model];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row > 0) {
        MKLineComicListModel *model = [_comicListArray objectAtIndex:indexPath.row - 1];
        MKLineComicBrowseViewController *browse = [[MKLineComicBrowseViewController alloc] init];
        NSString *urlString = [NSString stringWithFormat:@"http://m.ac.qq.com/event/light_comic/view.html?route=comicList-comicInfo&id=%@&cid=%@", _detailModel.detailID, model.cid];
        browse.url = urlString;
        [self.navigationController pushViewController:browse animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
